<template>
    <div class="coment">
       <h3>发表评论</h3>
       <hr>
       <textarea placeholder="评论数不能超过120" maxlength="120" v-model="info"></textarea>
        <mt-button type="primary" size="large" @click="addinfo">添加评论</mt-button>

        <div class="list">
            <div class="comm" v-for="(item,index) in listcomm" :key="index">
                <div class="title">
                    第{{index+1}}楼&nbsp;&nbsp;
                    用户:{{item.user_name}}
            &nbsp;&nbsp;发表时间:{{item.add_time|timeF}}
                </div>
                <div class="bob">
                   {{item.content=="undefined"?'用户很懒，什么都没留下':item.content}}
                </div>
            </div>
        </div>

        <mt-button type="primary" size="large" plain @click="getmore">查看更多</mt-button>
   
    </div>
</template>
<script>
import {getcommit,addcommit} from '@/api'
export default {
        data(){
            return{
                listcomm:[],
                page:1,
                info:''
            }
        },
        created () {
            this.getcom()
        },
        methods:{
            getcom(){
                getcommit({id:this.id,pageindex:this.page}).then(res=>{
                    console.log(res,12313);
                    this.listcomm =this.listcomm.concat(res.message);
                })
            },
            getmore(){
                this.page=this.page++
                this.getcom()
            },
            addinfo(){
                if(this.info.trim().length ==0){
                    return alert('评论不能为空')
                }
        
                addcommit({id:this.id,content:this.info.trim()}).then(res=>{
                    //  console.log(res,12313);
                    var cmt = {user_name:'匿名用户',add_time:Date.now(),content:this.info};
                   this.listcomm.unshift(cmt);
                   this.info=""
                })
            }
          
        },
        props: ['id'],
}
</script>
<style lang="scss" scoped>
    .coment{
        h3{
            font-size: 18px;
        }
        textarea{
            margin: 0;
            font-size: 15px;
        }
        .list{
             margin: 10px 0;
            .comm{
             font-size:13px;
             .title{
                 line-height:30px;
                background-color: #ccc;
             }
             .bob{
                 text-indent: 2rem;
                 line-height:30px;
             }
            }
        }
    }
</style>


